<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="店铺编号" prop="shopId">
        <el-input v-model="queryParams.shopId" placeholder="请输入店铺编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item label="" prop="shopName">
        <!-- <el-input v-model="queryParams.shopName" placeholder="请输入店铺名称" clearable @keyup.enter.native="handleQuery" /> -->
        <el-select v-model="queryParams.shopName" placeholder="店铺名称" clearable @change="getList">
          <el-option v-for="dict in dict.type.sys_product_live" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="" prop="outerPayId">
        <el-input v-model="queryParams.outerPayId" placeholder="请输入外部支付单号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="线上订单号" prop="soId">
        <el-input v-model="queryParams.soId" placeholder="请输入线上订单号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <!-- <el-form-item label="款式编码" prop="iId">
        <el-input v-model="queryParams.iId" placeholder="请输入款式编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item label="" prop="skuId">
        <el-input v-model="queryParams.skuId" placeholder="请输入商品编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="" prop="outerOiId">
        <el-input v-model="queryParams.outerOiId" placeholder="请输入子订单" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <!-- <el-form-item label="" prop="qty">
        <el-input v-model="queryParams.qty" placeholder="请输入数量" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="" prop="amount">
        <el-input v-model="queryParams.amount" placeholder="请输入总额" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item label="" prop="referrerName">
        <el-input v-model="queryParams.referrerName" placeholder="请输入达人名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="" prop="referrerId">
        <el-input v-model="queryParams.referrerId" placeholder="请输入达人uid" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="" prop="price">
        <el-input v-model="queryParams.price" placeholder="请输入单价" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <!-- <el-form-item label="" prop="sendDate">
        <el-date-picker clearable v-model="queryParams.sendDate" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择发货日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="" prop="sendDay">
        <el-date-picker clearable v-model="queryParams.sendDay" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择发货日期">
        </el-date-picker>
      </el-form-item> -->
      <!-- <el-form-item label="" prop="payDate">
        <el-date-picker clearable v-model="queryParams.payDate" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择支付时间">
        </el-date-picker>
      </el-form-item> -->
      <!-- <el-form-item label="" prop="payDay">
        <el-date-picker clearable v-model="queryParams.payDay" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择支付日期">
        </el-date-picker>
      </el-form-item> -->
      <!-- <el-form-item label="" prop="chosenChannel">
        <el-input v-model="queryParams.chosenChannel" placeholder="请输入实发快递渠道" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <!-- <el-form-item label="" prop="commission">
        <el-input v-model="queryParams.commission" placeholder="请输入佣金" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- <div class="desc">
      <timeSearch @getDateArr="getDateArr" @changDay="changDay" :timeArr="timeArr" :dayActive="30" :dayLimit="180" />
    </div> -->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5" style="display:none">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['product:ods:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5" style="display:none">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['product:ods:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5" style="display:none">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['product:ods:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['product:ods:export']">导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button size="mini" plain icon="el-icon-upload" @click="daoru">导入</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <div ref="tableDom">
      <el-table v-loading="loading" :maxHeight="odsList.length ? tableHeight : 'auto'" :data="odsList"
        @selection-change="handleSelectionChange">
        <!-- <el-table-column type="selection" width="55" align="center" /> -->
        <!-- <el-table-column label="ID" align="center" prop="id" /> -->
        <el-table-column label="店铺编号" align="center" prop="shopId" width="85" />
        <el-table-column label="店铺名称" align="center" prop="shopName" width="120" />
        <el-table-column label="平台订单状态" align="center" prop="shopStatus" width="140">
          <template slot-scope="scope">
            <div>
              {{
                scope.row.shopStatus | state1
              }}
            </div>

          </template>
        </el-table-column>
        <el-table-column label="外部支付单号" align="center" prop="outerPayId" width="200" />
        <!-- <el-table-column label="线上订单号" align="center" prop="soId" /> -->
        <!-- <el-table-column label="款式编码" align="center" prop="iId" /> -->
        <el-table-column label="商品编码" align="center" prop="skuId" width="150" />
        <el-table-column label="子订单" align="center" prop="outerOiId" width="180" />
        <el-table-column label="数量" align="center" prop="qty" />
        <el-table-column label="总额" align="center" prop="amount" />
        <el-table-column label="达人名称" align="center" prop="referrerName" width="150" />
        <el-table-column label="达人uid" align="center" prop="referrerId" width="150" />
        <el-table-column label="单价" align="center" prop="price" />
        <!-- <el-table-column label="发货日期" align="center" prop="sendDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sendDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
        <!-- <el-table-column label="发货日期" align="center" prop="sendDay" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sendDay, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
        <el-table-column label="支付时间" align="center" prop="payDate" width="120">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.payDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <!-- <el-table-column label="支付日期" align="center" prop="payDay" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.payDay, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
        <!-- <el-table-column label="实发快递渠道" align="center" prop="chosenChannel" /> -->
        <el-table-column label="佣金" align="center" prop="commission" />
        <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['product:ods:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['product:ods:remove']">删除</el-button>
        </template>
      </el-table-column> -->
      </el-table>
    </div>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改订单来源信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="店铺编号" prop="shopId">
          <el-input v-model="form.shopId" placeholder="请输入店铺编号" />
        </el-form-item>
        <el-form-item label="店铺名称" prop="shopName">
          <el-input v-model="form.shopName" placeholder="请输入店铺名称" />
        </el-form-item>
        <el-form-item label="外部支付单号" prop="outerPayId">
          <el-input v-model="form.outerPayId" placeholder="请输入外部支付单号" />
        </el-form-item>
        <el-form-item label="线上订单号" prop="soId">
          <el-input v-model="form.soId" placeholder="请输入线上订单号" />
        </el-form-item>
        <el-form-item label="款式编码" prop="iId">
          <el-input v-model="form.iId" placeholder="请输入款式编码" />
        </el-form-item>
        <el-form-item label="商品编码" prop="skuId">
          <el-input v-model="form.skuId" placeholder="请输入商品编码" />
        </el-form-item>
        <el-form-item label="子订单" prop="outerOiId">
          <el-input v-model="form.outerOiId" placeholder="请输入子订单" />
        </el-form-item>
        <el-form-item label="数量" prop="qty">
          <el-input v-model="form.qty" placeholder="请输入数量" />
        </el-form-item>
        <el-form-item label="总额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入总额" />
        </el-form-item>
        <el-form-item label="达人名称" prop="referrerName">
          <el-input v-model="form.referrerName" placeholder="请输入达人名称" />
        </el-form-item>
        <el-form-item label="达人uid" prop="referrerId">
          <el-input v-model="form.referrerId" placeholder="请输入达人uid" />
        </el-form-item>
        <el-form-item label="单价" prop="price">
          <el-input v-model="form.price" placeholder="请输入单价" />
        </el-form-item>
        <el-form-item label="发货日期" prop="sendDate">
          <el-date-picker clearable v-model="form.sendDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择发货日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="发货日期" prop="sendDay">
          <el-date-picker clearable v-model="form.sendDay" type="date" value-format="yyyy-MM-dd" placeholder="请选择发货日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="支付时间" prop="payDate">
          <el-date-picker clearable v-model="form.payDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择支付时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="支付日期" prop="payDay">
          <el-date-picker clearable v-model="form.payDay" type="date" value-format="yyyy-MM-dd" placeholder="请选择支付日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="实发快递渠道" prop="chosenChannel">
          <el-input v-model="form.chosenChannel" placeholder="请输入实发快递渠道" />
        </el-form-item>
        <el-form-item label="佣金" prop="commission">
          <el-input v-model="form.commission" placeholder="请输入佣金" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改产品二级信息对话框 :on-change="handleChange"-->
    <el-dialog title="导入" :visible.sync="dialogVisible" width="500px" append-to-body v-if="dialogVisible">
      <!-- <el-upload :action="Upload.url" :headers="Upload.headers" :limit="limit" :on-exceed="handleExceed"
        :on-success="handlePictureSuccessPic" :before-upload="beforeAvatarUpload" :on-preview="handlePictureCardPreview"
        :file-list="fileLi" :on-change="changeFile1">
        <el-button size="small" type="primary">导入文件</el-button> 
      </el-upload>-->
      <el-form :model="form" :rules="rules" ref="form" label-width="80px" style="width: 100%">
        <el-form-item label="时间" prop="time">
          <!-- <el-input v-model="form.time" placeholder="Enter time"></el-input>   -->
          <el-date-picker clearable v-model="form.time" type="date" value-format="yyyy-MM-dd" placeholder="请选择发货日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="店铺" prop="store_info">
          <!-- <el-input v-model="form.store_info" placeholder="Enter store info"></el-input>   -->
          <el-select v-model="form.store_info" placeholder="店铺名称" clearable>
            <el-option v-for="dict in dict.type.sys_product_live" :key="dict.value" :label="dict.label"
              :value="dict.value" />
          </el-select>
        </el-form-item>

        <el-form-item label="选择文件">

        </el-form-item>
        <!-- <el-button type="primary" @click="uploadExcel">Upload Excel</el-button>   -->

      </el-form>

    </el-dialog>
  </div>
</template>

<script>
import timeSearch from "./day.vue";
import { listOds, getOds, delOds, addOds, updateOds } from "@/api/sample/ods";

export default {

  name: "Ods",
  dicts: ['sys_product_live'],
  components: { timeSearch },
  data() {
    return {
      dialogVisible: false,
      dayActive: 30,
      timeArr: [{ value: '1', label: '今天' }, { value: '2', label: '昨天' }, { value: '7', label: '最近7天' }, { value: '30', label: '最近30天' }],
      timeGap: { startTime: "", endTime: "" },
      tableHeight: '',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单来源信息表格数据
      odsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        shopId: null,
        shopName: 'VFU-抖音小店',
        shopStatus: null,
        outerPayId: null,
        soId: null,
        iId: null,
        skuId: null,
        outerOiId: null,
        qty: null,
        amount: null,
        referrerName: null,
        referrerId: null,
        price: null,
        sendDate: null,
        sendDay: null,
        payDate: null,
        payDay: null,
        chosenChannel: null,
        commission: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        outerPayId: [
          { required: true, message: "外部支付单号不能为空", trigger: "blur" }
        ],
        soId: [
          { required: true, message: "线上订单号不能为空", trigger: "blur" }
        ],
        outerOiId: [
          { required: true, message: "子订单不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  filters: {
    state1(string) {
      if (string == 'WAIT_BUYER_PAY') { return "等待买家付款" };
      if (string == 'WAIT_SELLER_SEND_GOODS') { return "等待卖家发货" };
      if (string == 'WAIT_BUYER_CONFIRM_GOODS') { return "等待买家确认收货" };

      if (string == 'TRADE_FINISHED') { return "交易成功" };
      if (string == 'TRADE_CLOSED') { return "付款后交易关闭" };
      if (string == 'TRADE_CLOSED_BY_TAOBAO') { return "付款前交易关闭" };
      // if (int == 2) { return "审批通过" };
    },
  },
  mounted() {
    var _this = this
    this.resizeTable()
    window.onresize = function () {
      _this.resizeTable()
    }
  },
  //记得清空  如果在keepalive中 则使用activated  deactivated
  destroyed() {
    window.onresize = null
  },
  methods: {
    //部门文件导入修改打开
    daoru() {
      this.dialogVisible = true
    },
    changDay(val) {
      this.dayActive = val;
      this.timeGap.startTime = val[0];
      this.timeGap.endTime = val[1];
    },
    getDateArr(value) {
      this.timeGap.startTime = value[0];
      this.timeGap.endTime = value[1];
      console.log(JSON.stringify(this.timeGap));
    },
    /** 查询订单来源信息列表 */
    getList() {
      this.loading = true;
      listOds(this.queryParams).then(response => {
        this.odsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        shopId: null,
        shopName: null,
        shopStatus: null,
        outerPayId: null,
        soId: null,
        iId: null,
        skuId: null,
        outerOiId: null,
        qty: null,
        amount: null,
        referrerName: null,
        referrerId: null,
        price: null,
        sendDate: null,
        sendDay: null,
        payDate: null,
        payDay: null,
        chosenChannel: null,
        commission: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单来源信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOds(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单来源信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOds(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOds(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除订单来源信息编号为"' + ids + '"的数据项？').then(function () {
        return delOds(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('product/ods/export', {
        ...this.queryParams
      }, `ods_${new Date().getTime()}.xlsx`)
    },
    // table自动高度
    returnTableHeight(ref, bottomOffset) {
      let height = null
      height = window.innerHeight - ref.getBoundingClientRect().top - bottomOffset
      return height
    },


    resizeTable() {
      this.$nextTick(() => {
        this.tableHeight = this.returnTableHeight(this.$refs.tableDom, 100)
      }, 0)
    },
  }
};
</script>
